package com.example.smartcity.ui.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.smartcity.R;
import com.example.smartcity.ui.Bean.HospitalListViewBean;
import com.example.smartcity.ui.Tools.OkHttpUtils;

import java.util.List;

public class HospitalListViewAdapter extends ArrayAdapter<HospitalListViewBean.RowsBean> {
    private int resource;
    public HospitalListViewAdapter(@NonNull Context context, int resource, @NonNull List<HospitalListViewBean.RowsBean> objects) {
        super(context, resource, objects);
        this.resource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        HospitalListViewBean.RowsBean item = getItem(position);
        View view = null;
        ViewHolder viewHolder = null;
        if (convertView==null){
            view = LayoutInflater.from(getContext()).inflate(resource,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.TV_imgUrl = view.findViewById(R.id.TV_imgUrl);
            viewHolder.RB_level = view.findViewById(R.id.RB_level);
            viewHolder.TV_hospitalName = view.findViewById(R.id.TV_hospitalName);
            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.TV_hospitalName.setText(item.getHospitalName());
        viewHolder.RB_level.setRating(Float.valueOf(item.getLevel()));
        if (item.getImgUrl()!=null){
            Glide.with(getContext())
                    .load(OkHttpUtils.getBaseUrl(getContext())+item.getImgUrl())
                    .override(76,55)
                    .into(viewHolder.TV_imgUrl);
        }
        return view;
    }
    static class ViewHolder{
        ImageView TV_imgUrl;
        TextView TV_hospitalName;
        RatingBar RB_level;
    }
}
